package com.tang.cometTest.ation;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tang.cometTest.cache.UserCache;
import com.tang.cometTest.entity.LoginVO;
import com.tang.cometTest.service.UserService;

public class LoginAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserService userService;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		LoginVO vo = userService.login(username, password);
		if (vo != null) {
			UserCache.setUsersCatch(vo.getSessionID(), vo.getUsername());
			req.setAttribute("session", vo.getSessionID());
			req.setAttribute(username, vo.getUsername());
			req.getRequestDispatcher("main.jsp").forward(req, resp);
		} else {
			resp.sendRedirect("index.jsp");
		}
	}
}
